package com.wms.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.annotation.MyLog;
import com.wms.entity.BaseWarehouse;
import com.wms.entity.BaseWarehouseDTO;
import com.wms.listener.BaseWarehouseListener;
import com.wms.service.BaseWarehouseService;
import com.wms.util.DateUtils;
import com.wms.util.R;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author TianWenHe
 * @date 2025/4/28
 * @desc 仓库信息
 */
@RestController
@RequestMapping("/base/warehouse")
public class BaseWarehouseController {
    private Logger logger = LogManager.getLogger(BaseWarehouseController.class);

    @Autowired
    private BaseWarehouseService baseWarehouseService;

    /**
     * 分页查询仓库信息记录
     */
    @GetMapping("/find")
    @MyLog(value = "分页查询仓库信息记录", type = "查询")
    public R find(BaseWarehouseDTO baseWarehouseDTO) {
        logger.info("分页查询仓库信息记录");
        PageHelper.startPage(baseWarehouseDTO.getPageNum(), baseWarehouseDTO.getPageSize());
        List<BaseWarehouse> baseWarehouses = baseWarehouseService.find(baseWarehouseDTO);
        return R.ok(new PageInfo<>(baseWarehouses));
    }
    /**
     * 查询全部仓库的名字
     */
    @GetMapping("/namelist")
    @MyLog(value = "查询全部仓库名字", type = "查询")
    public R findName() {
        List<String> warehouseNames = baseWarehouseService.findName();
        return R.ok(warehouseNames);
    }

    /**
     * 新增仓库信息记录
     */
    @PostMapping("/add")
    @MyLog(value = "新增仓库信息记录", type = "新增")
    public R add(@RequestBody BaseWarehouse baseWarehouse) {
        logger.info("新增仓库信息记录");
        Long warehouseId = baseWarehouseService.add(baseWarehouse);
        return R.ok(warehouseId);
    }

    /**
     * 修改仓库信息记录
     */
    @PutMapping("/update")
    @MyLog(value = "修改仓库信息记录", type = "修改")
    public R update(@RequestBody BaseWarehouse baseWarehouse) {
        logger.info("修改仓库信息记录");
        baseWarehouseService.update(baseWarehouse);
        return R.ok();
    }

    /**
     * 批量删除仓库信息记录
     */
    @DeleteMapping("/delete")
    @MyLog(value = "批量删除仓库信息记录", type = "删除")
    public R delete(@RequestParam List<Long> ids) {
        logger.info("批量删除仓库信息记录");
        baseWarehouseService.delete(ids);
        return R.ok();
    }

    /**
     * 批量导入仓库信息记录
     */
    @PostMapping("/insertBatch")
    @MyLog(value = "批量导入仓库信息记录", type = "导入")
    public R insertBatch(MultipartFile file) throws IOException {
        logger.info("批量导入仓库信息记录");
        EasyExcel.read(file.getInputStream(), BaseWarehouse.class, new BaseWarehouseListener(baseWarehouseService)).sheet().doRead();
        return R.ok();
    }

    /**
     * 导出仓库信息记录
     */
    @GetMapping("/export")
    @MyLog(value = "导出仓库信息记录", type = "导出")
    public void export(HttpServletResponse response) throws IOException {
        logger.info("导出仓库信息记录");
        // 查询全部仓库信息记录
        List<BaseWarehouse> baseWarehouses = baseWarehouseService.find(new BaseWarehouseDTO());
        // 所有不为空的时间减8小时，这样Excel表中的时间才与数据库时间一致
        for (BaseWarehouse baseWarehouse : baseWarehouses) {
            if (baseWarehouse.getCreateTime() != null) {
                baseWarehouse.setCreateTime(DateUtils.subtractFixedEightHours(baseWarehouse.getCreateTime()));
            }
            if (baseWarehouse.getUpdateTime() != null) {
                baseWarehouse.setUpdateTime(DateUtils.subtractFixedEightHours(baseWarehouse.getUpdateTime()));
            }
        }
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("仓库信息记录", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), BaseWarehouse.class).autoCloseStream(Boolean.FALSE).sheet("仓库信息记录").doWrite(baseWarehouses);
    }
}
